/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qq.vip.mikuPy2001.bukkit.gui.privater;

import com.qq.vip.mikuPy2001.bukkit.gui.WindowsView;
import com.qq.vip.mikuPy2001.bukkit.gui.model.Module;
import com.qq.vip.mikuPy2001.bukkit.gui.util.ItemUtil;
import com.qq.vip.mikuPy2001.bukkit.gui.util.Message;
import org.bukkit.inventory.ItemStack;

/**
 *
 * @author MukiPy2001
 */
public abstract class PrivateModule implements Cloneable {

    private DefaultWindowsView view = null;
    private boolean enable;
    private ItemStack itemStack;

    /**
     * 获取关联到本控件的{@link WindowsView-}.
     *
     * @return
     */
    public final WindowsView getView() {
        return view;
    }

    /**
     * 控件是否激活
     *
     * @return
     */
    public final boolean isEnable() {
        return enable;
    }

    protected PrivateModule() {
    }

    final boolean isChange0() {
        ItemStack itemStackInView = ((Module) this).getItemStack();
        if (!ItemUtil.equals(itemStack, itemStackInView)) {
            return true;
        }
        return false;
    }

    final void enable0() {
        enable = true;
        Message.debug("激活|" + this);
        enable();
    }

    /**
     * 激活事件. 当控件被激活时此方法会被调用
     *
     */
    public abstract void enable();

    final void disable0() {
        enable = false;
        Message.debug("注销|" + this);
        disable();
    }

    /**
     * 注销事件. 当控件被激活时此方法会被调用
     *
     */
    public abstract void disable();

    /**
     * 获取缓存里的图标. 不推荐这么做. 建议使用{@link Module#getItemStack()}
     *
     * @return
     *
     * @see Module#getItemStack()
     */
    @Deprecated
    protected final ItemStack getItemStack0() {
        return itemStack;
    }

    /**
     * 设置缓存里的图标. 不推荐这么做.
     * 建议使用{@link Module#setItemStack(org.bukkit.inventory.ItemStack)}
     *
     * @param itemStack
     *
     * @see Module#setItemStack(org.bukkit.inventory.ItemStack)
     */
    @Deprecated
    protected final void setItemStack0(ItemStack itemStack) {
        this.itemStack = itemStack;
    }

    protected abstract void init();

    final Module clone(DefaultWindowsView view) {
        try {
            PrivateModule clone = (PrivateModule) clone();
            clone.view = view;
            Message.debug("初始化|" + clone);
            clone.init();
            return (Module) clone;
        } catch (CloneNotSupportedException ex) {
            Message.warn("A Exception in clone");
            throw new RuntimeException(ex);
        }
    }

}
